package com.odb.predis;

import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisClient {

	public static final Logger logger = LoggerFactory.getLogger(RedisClient.class);
	
	private static JedisPool pool;

	static {
		ResourceBundle bundle = ResourceBundle.getBundle("redis");
		if (bundle == null) {
			throw new IllegalArgumentException(
					"cannot find the redis.properties");
		}
		JedisPoolConfig config = new JedisPoolConfig();

		config.setMaxActive(Integer.parseInt(bundle
				.getString("redis.pool.maxActive")));
		config.setMaxIdle(Integer.parseInt(bundle
				.getString("redis.pool.maxIdle")));
		config.setMaxWait(Long.parseLong(bundle.getString("redis.pool.maxWait")));
		
		logger.info(bundle.getString("redis.server"));
		logger.info(bundle.getString("redis.port"));
		logger.info(bundle.getString("redis.overtime"));
		
		pool = new JedisPool(config, bundle.getString("redis.server"),
				Integer.valueOf(bundle.getString("redis.port")),
				Integer.parseInt(bundle.getString("redis.overtime")),
				bundle.getString("redis.password"));

	}
	public static Jedis getJedis(){
		return pool.getResource();
	}
	public static void endJedis(Jedis jedis){
		pool.returnResource(jedis);
	}
	
	public static void main(String[] args){
		Jedis jd = RedisClient.getJedis();
		
		jd.del("listDemo");
		//jd.llen("listDemo");
		jd.lpush("listDemo", "A"); ;
		jd.lpush("listDemo", "b");
		
		jd.rpop("listDemo");
		System.out.println(jd.lrange("listDemo", 0, -1));  
		
		RedisClient.endJedis(jd);
	}


}
